// Copyright (c) 2011 SameGoal LLC.
// All Rights Reserved.
// Author: Andy Hochhaus <ahochhaus@samegoal.com>

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef NET_HTTP_SERVER_COMET_JSON_H_
#define NET_HTTP_SERVER_COMET_JSON_H_

#include <gflags/gflags.h>
#include <glog/logging.h>

#include <map>
#include <string>

#include "base/basictypes.h"

namespace sg {
namespace net {
namespace http {
namespace server {
namespace comet {

class ItemRecord {
 public:
  enum ItemRecordType {
    UNKNOWN,
    STRING,
    ARRAY
  };

  ItemRecord();

  virtual std::string Serialize() = 0;

  virtual ~ItemRecord() {}

  ItemRecord *next;

 protected:
  ItemRecordType type_;

 private:
  DISALLOW_COPY_AND_ASSIGN(ItemRecord);
};

class JsonArray : public ItemRecord {
 public:
  JsonArray();

  void AppendString(const std::string &s);
  void AppendNumber(const double d);
  void AppendBoolean(const bool b);
  void AppendBytes(const std::string &s);
  JsonArray *AppendArray();

  std::string Serialize();

 private:
  void Append_(ItemRecord *item);

  ItemRecord *first_;
  ItemRecord *last_;

  DISALLOW_COPY_AND_ASSIGN(JsonArray);
};

class JsonString : public ItemRecord {
 public:
  JsonString(const std::string &s, const bool quote = false);

  std::string Serialize();

 private:
  std::string s_;

  DISALLOW_COPY_AND_ASSIGN(JsonString);
};

}  // namespace comet
}  // namespace server
}  // namespace http
}  // namespace net
}  // namespace sg

#endif  // NET_HTTP_SERVER_COMET_JSON_H_
